// 获取应用实例
const app = getApp();

Page({
  data: {
    score: 0,
    duration: '0分钟',
    correctRate: 0,
    statistics: {
      totalCount: 0,
      correctCount: 0,
      wrongCount: 0
    },
    wrongQuestions: []
  },

  onLoad(options) {
    // 获取考试ID
    const examId = options.id;
    if (!examId) {
      app.showError('参数错误');
      return;
    }
    this.loadExamResult(examId);
  },

  // 加载考试结果
  loadExamResult(examId) {
    app.showLoading();
    app.request({
      url: '/exam/result',
      method: 'GET',
      data: { examId }
    }).then(res => {
      app.hideLoading();
      if (res.code === 0) {
        const result = res.data;
        this.setData({
          score: result.score,
          duration: this.formatDuration(result.duration),
          correctRate: result.correctRate,
          statistics: result.statistics,
          wrongQuestions: result.wrongQuestions
        });
      }
    }).catch(() => {
      app.hideLoading();
      app.showError('获取考试结果失败');
    });
  },

  // 格式化时长
  formatDuration(seconds) {
    const minutes = Math.floor(seconds / 60);
    return `${minutes}分钟`;
  },

  // 跳转到题目详情
  goToQuestion(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/exam-question/detail/detail?id=${id}`
    });
  },

  // 查看解析
  goToReview() {
    wx.navigateTo({
      url: '/pages/exam-review/exam-review'
    });
  },

  // 加入错题本
  goToWrongBook() {
    wx.navigateTo({
      url: '/pages/wrong-book/wrong-book'
    });
  },

  // 返回首页
  goToHome() {
    wx.switchTab({
      url: '/pages/index/index'
    });
  }
}); 